
/***增删行开始*/
var i = 0;
function addtr(){
    i++;
    var trid = 'tr_'+i;
    var tr = $(".temptr").clone();
    tr.attr('id',trid).removeClass("temptr");
    tr.find("td:eq(1) select").empty().append('<option value="0">请选择种类</option>');
    tr.find("td:eq(2) input").val('');
    tr.find("td:eq(3)").empty().append(' <i class="icon-remove deltr" onclick="deltr(\''+trid+'\')"></i>');

    $('#detailtable').append(tr);
}
function deltr(trid){
    layer.confirm('你确定要删除该行吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        layer.msg('删除成功', {icon: 1});
        $("#"+trid).remove();
    });

}
/***增删行结束*/
var jsonArray = new Array();
var totalmoney = 0;

$(function(){

    /*<![CDATA[*/
    $("#submit").on('click',function(){

        /**提交表单的时候必须计算总价**/
        cal_total();
        console.info(jsonArray);
        $.ajax({
            url:"/order/save",
            type:"post",
            dataType:'json',
            data:$("#form-add").serialize(),
            success:function(data){

                if(data ==1){
                    var index = layer.index;
                    location.replace(location.href);
                    layer.close(index);
                }
            }
        });
    });

});
function addJson(json, key, value){
    return json[key] = value;
}
//点击之后从后台load商品
function loadGoods(obj){
    layer.msg($(obj).val(),{time:1000});

    var brotherTr = $(obj).parents("tr").find("td:eq(1) select");
    brotherTr.empty();
    var cid = $(obj).val();
    if(cid){
        $.get('/order/getGoodsByCid',{cid:cid},function (data) {

            $.each(data.data,function(index,value){
                var option = '<option price="'+value.price+'"  value="'+value.id+'">'+value.name+'</option>';
                brotherTr.append(option);
            });
        })
    }
}
//给焦点计算总价
function inputfocus(obj){

    $(obj).parents("tr").find("td:eq(2) input").focus();

}

function cal_total(){

    //计算之前先清空数组
    jsonArray.length = 0;
    totalmoney =0;
    $("#detailtable tr").each(function(trindex,tritem){
        var goods_c = $(tritem).find("td:eq(0) select").val() || 0; //产品分类
        var goods_n = $(tritem).find("td:eq(1) select"); //产品名称
        var goods_q = $(tritem).find("td:eq(2) input").val() || 0; //产品数量

        if(goods_c < 1 ){
            layer.alert("添加的商品必须选择分类");
            return false;
        }
        if(!goods_q || goods_q==0){
            layer.alert("商品数量填写错误");
            return false;
        }
        var jsonDetail = {};
        //验证通过则构建json数据
        addJson(jsonDetail,'goodsid',goods_n.val());
        addJson(jsonDetail,'goodscid',goods_c);
        addJson(jsonDetail,'goodsname',trim(goods_n.find("option:selected").text()));
        addJson(jsonDetail,'price',goods_n.find("option:selected").attr("price"));
        addJson(jsonDetail,'quantity',goods_q);
        jsonArray.push(jsonDetail);
    });

    /**用户可自行选择是否计算总价*/

    $.each(jsonArray,function (i,v) {
        totalmoney += parseInt(v.quantity) * parseFloat(v.price);
    });
    $("#totalmoney").val(totalmoney.toFixed(2));


    var orderDetailJson = JSON.stringify(jsonArray);
    $("#orderDetailJson").val(orderDetailJson);

}

/*]]>*/



